let monitoredRequests = [];
let isMonitoring = false;
let targetUrl = '';

// 开始监控
function startMonitoring(url) {
    isMonitoring = true;
    targetUrl = url;
    monitoredRequests = [];
}

// 停止监控
function stopMonitoring() {
    isMonitoring = false;
    chrome.storage.local.set({monitoredRequests});
}

// 监听请求
chrome.webRequest.onBeforeSendHeaders.addListener(
    function (details) {
        if (isMonitoring && details.url.startsWith(targetUrl)) {
            const request = {
                id: details.requestId,
                url: details.url,
                method: details.method,
                headers: details.requestHeaders || [],
                time: new Date().toLocaleString(),
            };
            monitoredRequests.push(request);
        }
    },
    {urls: ["<all_urls>"]},
    ["requestHeaders", "extraHeaders"]
);

// 接收消息
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
    if (msg.action === 'startMonitoring') {
        startMonitoring(msg.url);
    } else if (msg.action === 'stopMonitoring') {
        stopMonitoring();
    }
});
